<?php

/**
 * This is the model class for table "otheroutcomes".
 *
 * The followings are the available columns in table 'otheroutcomes':
 * @property integer $id
 * @property string $outcome
 * @property double $sum
 * @property string $paydate
 *
 * The followings are the available model relations:
 * @property Otheroutcome $ooutcome
 */
class Otheroutcomes extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Otheroutcomes the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'otheroutcomes';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('outcome, sum, paydate', 'required'),
			array('sum', 'numerical'),
			array('outcome', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, outcome, sum, paydate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'ooutcome' => array(self::BELONGS_TO, 'Otheroutcome', 'outcome'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'outcome' => 'Outcome',
			'sum' => 'Sum',
			'paydate' => 'Paydate',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array('with'=>'ooutcome'));

		$criteria->compare('id',$this->id);
		$criteria->compare('outcome',$this->outcome,true);
		$criteria->compare('sum',$this->sum);
		$criteria->compare('paydate',$this->paydate,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getTotals($provider)
	{
		$totals = array();
		$sumoutcomes = 0;
		$sumoutcomescnt = 0;
		$data = $provider->getData();
		foreach($data as $p)
		{	
			$sumoutcomes += $p->sum;
			$sumoutcomescnt++;
		}
		$totals['sumoutcomes']=$sumoutcomes;
		$totals['sumoutcomescnt']=$sumoutcomescnt;

		return $totals;
	}
}